package org.csu.mypetstore.api.controller.front;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Category;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/catalog/")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;


    @GetMapping("categories")
    @ResponseBody      // 把返回的对象变为Json对象
    //获取categories
    public CommonResponse<List<Category>> getCategoryList(){
        return catalogService.getCategoryList();
    }

    @GetMapping("categories/{id}")
    @ResponseBody
    //根据Id获取category
    public CommonResponse<Category> getCategoryById(@PathVariable("id") String categoryId){
        return catalogService.getCategoryById(categoryId);
    }

    /*   暂时用不到(可能一直用不到

    @PutMapping("categories/{id}")
    @ResponseBody
    //根据Id更新category
    public CommonResponse<Category> updateCategoryById(@PathVariable("id") Category category){
        return catalogService.updateCategoryById(category);
    }

    @PostMapping("categories/{id}")
    @ResponseBody
    //增加category
    public CommonResponse<Category> insertCategory(@PathVariable("id") String categoryId,String name,String description){
        return catalogService.insertCategory(categoryId,name,description);
    }

     */

    @GetMapping("categories/{id}/products")
    @ResponseBody
    //根据categoryId获取products
    public CommonResponse<List<Product>> getProductByCatid(@PathVariable("id") String categoryId){
        return catalogService.getProductByCatid(categoryId);
    }

    @GetMapping("products/{id}")
    @ResponseBody
    //根据productId获取product
    public CommonResponse<Product> getProductByProid(@PathVariable("id") String productId){
        return catalogService.getProductByProid(productId);
    }

    @GetMapping("likedProduct/{id}")
    @ResponseBody
    //根据模糊查询获取product
    public CommonResponse<List<Product>> getProductLikeProid(@PathVariable("id") String productId){
        return catalogService.getProductLikeProid(productId);
    }


    @GetMapping("products/{id}/items")
    @ResponseBody
    //根据productId获取items
    public CommonResponse<List<Item>> getItemByProid(@PathVariable("id") String productId){
        return catalogService.getItemByProid(productId);
    }

    @GetMapping("items/{id}")
    @ResponseBody
    //根据itemId获取item
    public CommonResponse<Item> getItemByItemid(@PathVariable("id") String itemId){
        return catalogService.getItemByItemid(itemId);
    }

    @GetMapping("itemVOs/{id}")
    @ResponseBody
    //根据itemId获取itemVO
    public CommonResponse<ItemVO> getItemByItemVOid(@PathVariable("id") String itemId){
        return catalogService.getItemByItemVOid(itemId);
    }

    @GetMapping("products/{id}/itemVOs")
    @ResponseBody
    //根据productId获取Item总信息
    public CommonResponse<List<ItemVO>> getCategoryByProductId(@PathVariable("id") String productId){
        return catalogService.getItemVOByProductId(productId);
    }
}
